/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.nostra13.sample.provider;

import com.nostra13.sample.ResourceTable;
import com.nostra13.sample.constant.Constants;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageSize;
import com.nostra13.universalimageloader.core.display.CircleBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.SimpleImageLoadingListener;
import com.nostra13.universalimageloader.utils.L;
import ohos.agp.components.*;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.media.image.PixelMap;
import ohos.media.image.common.PixelFormat;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

public class GalleryProvider extends BaseItemProvider {

    private Context mContext;
    private String[] mImageData = Constants.IMAGES;
    private LayoutScatter inflater;
    private DisplayImageOptions options;

    public GalleryProvider(Context context) {
        mContext = context;
        inflater = LayoutScatter.getInstance(context);
        options = new DisplayImageOptions.Builder()
                .showImageOnLoading(ResourceTable.Media_ic_stub)
                .showImageForEmptyUri(ResourceTable.Media_ic_empty)
                .showImageOnFail(ResourceTable.Media_ic_error)
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(PixelFormat.RGB_565)
                .displayer(new RoundedBitmapDisplayer(20))
                .build();

    }

    @Override
    public int getCount() {
        return mImageData.length;
    }

    @Override
    public Object getItem(int i) {
        return mImageData[i];
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public Component getComponent(int i, Component convertView, ComponentContainer componentContainer) {
        Component view = convertView;
        final ViewHolder holder;
        if (convertView == null) {
            view = inflater.parse(ResourceTable.Layout_item_gallery, componentContainer, false);
            holder = new ViewHolder();
            holder.text = (Text) view.findComponentById(com.nostra13.sample.ResourceTable.Id_text);
            holder.image = (Image) view.findComponentById(ResourceTable.Id_image);
            view.setTag(holder);
        } else {
            holder = (ViewHolder) view.getTag();
        }
        holder.text.setText("Item " + (i + 1));
        ImageLoader.getInstance().displayImage(mImageData[i],holder.image,options);

        return view;
    }

    static class ViewHolder {
        Text text;
        Image image;
    }
}
